package com.btnhom;

import java.awt.EventQueue;
import java.awt.Font;
import java.text.ParseException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class JF_Calculator extends JFrame implements ActionListener{

	private JPanel contentPane;
	private IntegerTextField txtNumber;
	final int MAX_INPUT_LENGTH = 20;
	final int INPUT_MODE = 0;
	final int RESULT_MODE = 1;
	final int ERROR_MODE = 2;
	int displayMode;

	boolean clearOnNextDigit, percent;
	double lastNumber;
	String lastOperator;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					
					JF_Calculator frame = new JF_Calculator();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * @throws ParseException 
	 */
	public JF_Calculator() throws ParseException {

		setTitle("Calculator");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 360, 325);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JButton btt_0 = new JButton("0");
		btt_0.addActionListener(this);
		btt_0.setBounds(10, 244, 121, 31);
		contentPane.add(btt_0);
		
		JButton btt_1 = new JButton("1");
		btt_1.addActionListener(this);
		btt_1.setBounds(10, 201, 54, 31);
		contentPane.add(btt_1);
		
		JButton btt_2 = new JButton("2");
		btt_2.setBounds(74, 201, 54, 31);
		contentPane.add(btt_2);
		
		JButton btt_3 = new JButton("3");
		btt_3.setBounds(141, 201, 54, 31);
		contentPane.add(btt_3);
		
		JButton btt_4 = new JButton("4");
		btt_4.setBounds(10, 159, 54, 31);
		contentPane.add(btt_4);
		
		JButton btt_5 = new JButton("5");
		btt_5.setBounds(74, 159, 54, 31);
		contentPane.add(btt_5);
		
		JButton btt_6 = new JButton("6");
		btt_6.setBounds(141, 159, 54, 31);
		contentPane.add(btt_6);
		
		JButton btt_7 = new JButton("7");
		btt_7.setBounds(10, 119, 54, 31);
		contentPane.add(btt_7);
		
		JButton btt_8 = new JButton("8");
		btt_8.setBounds(74, 119, 54, 31);
		contentPane.add(btt_8);
		
		JButton btt_9 = new JButton("9");
		btt_9.setBounds(141, 119, 54, 31);
		contentPane.add(btt_9);
		
		JButton btt_CE = new JButton("CE");
		btt_CE.setBounds(74, 77, 54, 31);
		contentPane.add(btt_CE);
		
		JButton btt_C = new JButton("C");
		btt_C.setBounds(141, 77, 54, 31);
		contentPane.add(btt_C);
		
		JButton btt_1x = new JButton("1/x");
		btt_1x.setBounds(279, 159, 54, 31);
		contentPane.add(btt_1x);
		
		JButton btt_dec = new JButton(".");
		btt_dec.setBounds(141, 244, 54, 31);
		contentPane.add(btt_dec);
		
		JButton bttNP = new JButton("+/-");
		bttNP.setBounds(211, 77, 54, 31);
		contentPane.add(bttNP);
		
		JButton btt_D = new JButton("/");
		btt_D.setBounds(211, 119, 54, 31);
		contentPane.add(btt_D);
		
		JButton btt_P = new JButton("+");
		btt_P.setBounds(211, 244, 54, 31);
		contentPane.add(btt_P);
		
		JButton btt_S = new JButton("-");
		btt_S.setBounds(211, 201, 54, 31);
		contentPane.add(btt_S);
		
		JButton btt_M = new JButton("*");
		btt_M.setBounds(211, 159, 54, 31);
		contentPane.add(btt_M);
		
		JButton btt_Sqrt = new JButton("\u221A");
		btt_Sqrt.setBounds(279, 77, 54, 31);
		contentPane.add(btt_Sqrt);
		
		JButton btt_Percent = new JButton("%");
		btt_Percent.setBounds(279, 119, 54, 31);
		contentPane.add(btt_Percent);
		
		JButton btt_E = new JButton("=");
		btt_E.setBounds(279, 201, 54, 74);
		contentPane.add(btt_E);
		
		txtNumber = new IntegerTextField();
		txtNumber.setText("0");
		txtNumber.setHorizontalAlignment(SwingConstants.RIGHT);
		txtNumber.setFont(new Font("Tahoma", Font.BOLD, 14));
		txtNumber.setBounds(10, 11, 324, 55);
		contentPane.add(txtNumber);
		
	}
	private Object CheckInteger(double number){
		boolean isInt = number == (int)number;
		if(isInt)
			return (int)number;
		else
			return number;
	}

	private void setDisplayString(String s){
		txtNumber.setText(s);
	}
	
	private String getDisplayString (){
		return txtNumber.getText();
	}
	
	void addDigitToDisplay(int digit){
		if (clearOnNextDigit)
			setDisplayString("");

		String inputString = getDisplayString();
		
		if (inputString.indexOf("0") == 0){
			inputString = inputString.substring(1);
		}

		if ((!inputString.equals("0") || digit > 0)  && inputString.length() < MAX_INPUT_LENGTH){
			setDisplayString(inputString + digit);
		}
		

		displayMode = INPUT_MODE;
		clearOnNextDigit = false;
	}
	
	
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		double result = 0;
		JButton s=(JButton)e.getSource();
		JOptionPane.showMessageDialog(rootPane, s.getText());
		switch (s.getText()) {
		case "0":
				addDigitToDisplay(0);
			break;
		case "1":
			addDigitToDisplay(0);
			break;
		case "btt_2":
			addDigitToDisplay(0);
			break;
		case "btt_3":
			addDigitToDisplay(0);
			break;
		case "btt_4":
			addDigitToDisplay(0);
			break;
		case "btt_5":
			addDigitToDisplay(0);
			break;
		case "btt_6":
			addDigitToDisplay(0);
			break;
		case "btt_7":
			addDigitToDisplay(0);
			break;
		case "btt_8":
			addDigitToDisplay(0);
			break;
		case "btt_9":
			addDigitToDisplay(0);
			break;
		default:
			break;
		}
		
		
	}
	


	
}